Imports Janus.Windows.GridEX
Imports System.IO

Public Class frmSIMPLE
    Dim CustomerCode As String = ""
    Dim EmployeeCode As String = ""
    Dim BILL_CODE As Integer = 0
    Dim PRO_CODE As String = ""
    Dim PRO_CODE_LINE As String = ""
#Region "Control Event"

    Private Sub frmSIMPLE_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            GC.Collect()
            Me.Dispose()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub frmSIMPLE_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PublicFunction.NewFormLoad(Me)

        lblBarcode.Top = pnDetail.Top
        lblBarcode.Height = pnDetail.CaptionHeight
        lblBarcode.Left = pnDetail.Left

        txtBarcode.Top = pnDetail.Top
        txtBarcode.Left = pnDetail.Left + lblBarcode.Width
        txtBarcode.Height = pnDetail.CaptionHeight

        lblScanner.Top = pnDetail.Top
        lblScanner.Height = pnDetail.CaptionHeight
        lblScanner.Left = pnDetail.Left + lblBarcode.Width + txtBarcode.Width
        lblScanner.Width = pnDetail.Width - lblBarcode.Width - txtBarcode.Width

        Me.GridDetail.RootTable.Columns("QUANTITY").FormatString = PublicVariable.NumberFormatString
        Me.GridDetail.RootTable.Columns("TOTAL_ORG").FormatString = PublicVariable.NumberFormatString
        Me.GridDetail.RootTable.Columns("PRICE").FormatString = PublicVariable.NumberFormatString
        Me.GridDetail.RootTable.Columns("DSC_PER").FormatString = PublicVariable.NumberFormatString
        Me.GridDetail.RootTable.Columns("DSC_AMT").FormatString = PublicVariable.NumberFormatString
        If File.Exists(PublicVariable.LogoFile) Then
            PbLogo.BackgroundImage = New Bitmap(PublicVariable.LogoFile)
        End If
    End Sub

    Private Sub btnNewCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewCustomer.Click
        Dim frm As New frmCUSTOMER_DETAIL
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then

        End If
    End Sub

    Private Sub btnSearchCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchCustomer.Click
        Dim frm As New frmSEARCH_CUSTOMER
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            CustomerCode = frm.GridCustomer.GetValue("CUS_CODE")
            txtCustomerName.Text = frm.GridCustomer.GetValue("CUS_NAME")
        End If
    End Sub

    Private Sub btnSearchItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchItem.Click
        Dim frm As New frmSEARCH_ITEM
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            If Not frm.GridItem.CurrentRow Is Nothing Then
                Dim dr As DataRowView = CType(frm.GridItem.CurrentRow.DataRow, DataRowView)
                AddItem(dr("ITEM_TYPE"), dr("ITEM_CODE").ToString, _
                           dr("ITEM_NAME").ToString, dr("PRICE"), _
                           dr("VAT_RATE"), dr("ATTRIBUTE").ToString)

            End If
        End If
    End Sub

    Private Sub btnAdmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm As New frmMANAGEMENT
        frm.ShowDialog()
    End Sub

    Private Sub btnOpenOrderList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenOrderList.Click
        Dim frm As New frmTOGOLIST
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then

        End If
    End Sub

    Private Sub btnDetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetail.Click
        Dim frm As New frmCUSTOMER_DETAIL
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then

        End If
    End Sub

    Private Sub txtBarcode_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBarcode.KeyDown
        Try
            If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
                If Not IsNumeric(PublicVariable.BarcodeItem) Or _
                    Not IsNumeric(PublicVariable.BarcodeEmployee) Or _
                    Not IsNumeric(PublicVariable.BarcodeCustomer) Then
                    SystemFunction.ShowMessageForm("THE BARCODE LENGTH IS NOT NUMERIC!", "")
                    Return
                End If
                '-------------------------------------SEARCH ITEM------------------------
                If txtBarcode.Text.Length = CInt(PublicVariable.BarcodeItem) Then
                    Dim dt As DataTable
                    Dim arName As New ArrayList
                    arName.Add("@Condition")
                    arName.Add("@Parameter")
                    Dim arVal As New ArrayList
                    arVal.Add(3)
                    arVal.Add(txtBarcode.Text)
                    dt = BLL.ObjectGetBy("ITEM_SEARCH", arName, arVal)

                    If dt.Rows.Count = 1 Then
                        'IF return only 1 record, add to grid
                        AddItem(dt.Rows(0).Item("ITEM_TYPE"), dt.Rows(0).Item("ITEM_CODE").ToString, _
                            dt.Rows(0).Item("ITEM_NAME").ToString, dt.Rows(0).Item("PRICE"), _
                            dt.Rows(0).Item("VAT_RATE"), dt.Rows(0).Item("ATTRIBUTE").ToString)
                    ElseIf dt.Rows.Count > 1 Then
                        'if return more than 1 record, show the selection
                        Dim frm As New frmSEARCH_ITEM
                        frm.GridItem.DataSource = dt.DefaultView
                        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
                            If frm.GridItem.CurrentRow Is Nothing Then Return
                            Dim dr As DataRowView = CType(frm.GridItem.CurrentRow.DataRow, DataRowView)
                            AddItem(dr.Item("ITEM_TYPE"), dr.Item("ITEM_CODE").ToString, _
                                    dr.Item("ITEM_NAME").ToString, dr.Item("PRICE"), _
                                    dr.Item("VAT_RATE"), dr.Item("ATTRIBUTE").ToString)
                        End If
                    Else
                        'not found
                        SystemFunction.ShowMessageForm("ITEM NOT FOUND!", "")
                    End If
                End If
                '-------------------------------------SEARCH EMPLOYEE------------------------
                If txtBarcode.Text.Length = CInt(PublicVariable.BarcodeEmployee) Then
                    Dim dt As DataTable
                    dt = BLL.ObjectGetAll("EMPLOYEE_LOADALL")
                    Dim dv As New DataView(dt)
                    dv.RowFilter = "EMP_CARD='" + txtBarcode.Text + "'"
                    If dv.Count = 1 Then
                        EmployeeCode = dv(0).Item("EMP_CODE").ToString
                        txtEmployee.Text = dv(0).Item("EMP_NAME").ToString
                    ElseIf dv.Count > 1 Then
                        Dim frm As New frmEMPLOYEE
                        frm.GridEmployee.DataSource = dv
                        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
                            If frm.GridEmployee.CurrentRow Is Nothing Then Return
                            Dim dr As DataRowView = CType(frm.GridEmployee.CurrentRow.DataRow, DataRowView)
                            EmployeeCode = dr.Item("EMP_CODE").ToString
                            txtEmployee.Text = dr.Item("EMP_NAME").ToString
                        End If
                    Else
                        'not found
                        SystemFunction.ShowMessageForm("EMPLOYEE NOT FOUND!", "")
                    End If
                End If
                '-------------------------------------SEARCH CUSTOMER------------------------
                If txtBarcode.Text.Length = CInt(PublicVariable.BarcodeCustomer) Then
                    Dim arName As New ArrayList
                    arName.Add("@Condition")
                    arName.Add("@Parameter")
                    Dim arVal As New ArrayList
                    arVal.Add(5)
                    arVal.Add(txtBarcode.Text)
                    Dim dt As DataTable = BLL.ObjectGetBy("CUSTOMER_SEARCH", arName, arVal)
                    If dt.Rows.Count = 1 Then
                        'if result is 1 record
                        CustomerCode = dt.Rows(0).Item("CUS_CODE").ToString
                        txtCustomerName.Text = dt.Rows(0).Item("CUS_NAME").ToString
                    ElseIf dt.Rows.Count > 1 Then
                        'if Result is more than 1 record
                        Dim frm As New frmSEARCH_CUSTOMER
                        frm.GridCustomer.DataSource = dt.DefaultView
                        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
                            If frm.GridCustomer.CurrentRow Is Nothing Then Return
                            Dim dr As DataRowView = CType(frm.GridCustomer.CurrentRow.DataRow, DataRowView)
                            CustomerCode = dr.Item("CUS_CODE").ToString
                            txtCustomerName.Text = dr.Item("CUS_NAME").ToString
                        End If
                    Else
                        'not found
                        SystemFunction.ShowMessageForm("CUSTOMER NOT FOUND!", "")
                    End If
                End If
            End If
            txtBarcode.Text = ""
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub btnRemoveCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveCustomer.Click
        CustomerCode = ""
        txtCustomerName.Text = ""
    End Sub

    Private Sub btnRemoveEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveEmployee.Click
        EmployeeCode = ""
        txtEmployee.Text = ""
    End Sub

    Private Sub btnNewOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewOrder.Click
        NewOrder()
    End Sub


    Private Sub btnCompleteOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompleteOrder.Click
        If BILL_CODE > 0 Then
            'Update bill
            UpdateOrder()
        Else
            'Create bill
            BILL_CODE = CreateOrder()
        End If
        NewOrder()
    End Sub



    Private Sub GridDetail_ColumnButtonClick(ByVal sender As System.Object, ByVal e As Janus.Windows.GridEX.ColumnActionEventArgs) Handles GridDetail.ColumnButtonClick
        GridDetail.CurrentRow.Delete()
    End Sub
#End Region
#Region "Functions"
    Private Function CreateOrder() As Integer
        GridDetail.ExpandRecords()
        Dim PARENT_CODE As String = ""
        BILL_CODE = oOrder.BillAdd(CustomerCode, PublicVariable.SystemDate, txtSubTotal.Text, txtTax.Text, _
        0, 0, 0, txtTotal.Text, EmployeeCode, 1, "", PublicVariable.UserCode, "", 0, PRO_CODE, 1, txtNotes.Text, 0)
        If BILL_CODE > 0 Then
            For i As Integer = 0 To GridDetail.RowCount - 1
                GridDetail.Row = i
                If GridDetail.CurrentRow.RowType = RowType.Record Then
                    Dim ItemCode As String = GridDetail.GetValue("ITEM_CODE").ToString
                    Dim ItemName As String = GridDetail.GetValue("ITEM_NAME").ToString
                    Dim Quantity As Integer = GridDetail.GetValue("QUANTITY")
                    Dim DiscountPercent As Double = GridDetail.GetValue("DSC_PER")
                    Dim DiscountAmount As Double = GridDetail.GetValue("DSC_AMT")
                    Dim VAT_Rate As Double = GridDetail.GetValue("VAT_PER")
                    Dim VAT_Amt As Double = GridDetail.GetValue("VAT_AMT")
                    Dim Memo As String = GridDetail.CurrentRow.Cells("MEMO").Text
                    Dim KEEP_PACK As String = GridDetail.CurrentRow.Cells("KEEP_PACK").Text
                    Dim Price As Double = GridDetail.GetValue("PRICE")
                    Dim TOTAL_ORG As Double = GridDetail.GetValue("TOTAL_ORG")
                    Dim SUBITEM_CODE As String = GridDetail.CurrentRow.Cells("SUBITEM_CODE").Text

                    Dim BD_Code As Integer = 0
                    If SUBITEM_CODE = "" Then
                        BD_Code = oOrder.BillDetailAdd(BILL_CODE, ItemCode, ItemName, Price, _
                        Quantity, DiscountPercent, _
                        VAT_Rate, KEEP_PACK, _
                        Memo, "", PublicVariable.UserCode, SUBITEM_CODE, "", PRO_CODE_LINE, "")
                    Else
                        BD_Code = oOrder.BillDetailAdd(BILL_CODE, ItemCode, ItemName, Price, _
                                                Quantity, DiscountPercent, _
                                                VAT_Rate, KEEP_PACK, _
                                                Memo, "", PublicVariable.UserCode, SUBITEM_CODE, PARENT_CODE, PRO_CODE_LINE, "")
                    End If

                    If SUBITEM_CODE = "" Then 'Save BD_CODE for father item
                        PARENT_CODE = BD_Code
                    End If
                    If BD_Code <= 0 Then
                        MessageBox.Show("FAILED")
                        Return -1
                    End If
                End If
            Next
            Return BILL_CODE
        Else
            Return -1
        End If
    End Function
    Private Sub UpdateOrder()
        GridDetail.ExpandRecords()
        Dim PARENT_CODE As String
        oOrder.BillUpdate(CustomerCode, PublicVariable.SystemDate, EmployeeCode, 1, "", PublicVariable.UserCode, BILL_CODE, 0, PRO_CODE, 1, Me.txtNotes.Text, 0)
        For i As Integer = 0 To GridDetail.RowCount - 1
            GridDetail.Row = i
            If GridDetail.CurrentRow.RowType = RowType.Record Then
                Dim ItemCode As String = GridDetail.GetValue("ITEM_CODE").ToString
                Dim ItemName As String = GridDetail.GetValue("ITEM_NAME").ToString
                Dim Quantity As Integer = GridDetail.GetValue("QUANTITY")
                Dim DiscountPercent As Double = GridDetail.GetValue("DSC_PER")
                Dim DiscountAmount As Double = GridDetail.GetValue("DSC_AMT")
                Dim VAT_Rate As Double = GridDetail.GetValue("VAT_PER")
                Dim VAT_Amt As Double = GridDetail.GetValue("VAT_AMT")
                Dim Memo As String = GridDetail.CurrentRow.Cells("MEMO").Text
                Dim KEEP_PACK As String = GridDetail.CurrentRow.Cells("KEEP_PACK").Text
                Dim Price As Double = GridDetail.GetValue("PRICE")
                Dim TOTAL_ORG As Double = GridDetail.GetValue("TOTAL_ORG")
                Dim BD_Code As Integer = GridDetail.GetValue("BD_CODE")
                Dim SUBITEM_CODE As String = GridDetail.CurrentRow.Cells("SUBITEM_CODE").Text

                If BD_Code > 0 Then
                    oOrder.BillDetailUpdate(BD_Code, ItemCode, ItemName, Price, _
                        Quantity, DiscountPercent, _
                        VAT_Rate, KEEP_PACK, _
                        Memo, "", PublicVariable.UserCode, "", "")
                Else
                    If SUBITEM_CODE = "" Then
                        BD_Code = oOrder.BillDetailAdd(BILL_CODE, ItemCode, ItemName, Price, _
                        Quantity, DiscountPercent, _
                        VAT_Rate, KEEP_PACK, _
                        Memo, "", PublicVariable.UserCode, SUBITEM_CODE, "", PRO_CODE_LINE, "")
                    Else
                        BD_Code = oOrder.BillDetailAdd(BILL_CODE, ItemCode, ItemName, Price, _
                        Quantity, DiscountPercent, VAT_Rate, KEEP_PACK, _
                        Memo, "", PublicVariable.UserCode, SUBITEM_CODE, PARENT_CODE, PRO_CODE_LINE, "")
                    End If

                    If SUBITEM_CODE = "" Then 'Save BD_CODE for father item
                        PARENT_CODE = BD_Code
                    End If
                    If BD_Code > 0 Then
                        GridDetail.SetValue("BD_CODE", BD_Code)
                    Else
                        MessageBox.Show("FAILED")
                    End If
                End If

            End If
        Next
    End Sub
    Private Sub AddItem(ByVal ITEM_TYPE As Integer, ByVal ITEM_CODE As String, _
    ByVal ITEM_NAME As String, ByVal PRICE As Double, ByVal VAT_PER As Double, ByVal ATTRIBUTE As String)

        Dim dt As DataTable
        Dim arName As New ArrayList
        arName.Add("@ITEM_CODE")
        arName.Add("@CUS_CODE")
        arName.Add("@Date")
        Dim arVal As New ArrayList
        arVal.Add(ITEM_CODE)
        arVal.Add(CustomerCode)
        arVal.Add(PublicVariable.SystemDate)
        dt = BLL.ObjectGetBy("PROMOTION_LOAD", arName, arVal)

        If dt.Rows.Count > 0 Then
            Dim frm As New frmPROMOTION
            frm.GridPromotion.DataSource = dt.DefaultView
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                'ADD PROMOTION ITEM
                If frm.GridPromotion.CurrentRow.RowType = RowType.GroupHeader Then
                    frm.GridPromotion.Row = frm.GridPromotion.Row + 1
                End If
                Dim dv As New DataView(dt)
                dv.RowFilter = "PRO_CODE='" + frm.GridPromotion.GetValue("PRO_CODE").ToString + "' AND PRO_GROUP='" + frm.GridPromotion.GetValue("PRO_GROUP").ToString + "'"
                For Each dr As DataRowView In dv
                    PRICE = dr.Item("NEW_PRICE") / (1 + VAT_PER / 100)
                    Dim newRow As GridEXRow = GridDetail.AddItem()
                    newRow.BeginEdit()
                    newRow.Cells("ITEM_CODE").Value = dr.Item("ITEM_CODE").ToString
                    newRow.Cells("ITEM_NAME").Value = dr.Item("ITEM_NAME").ToString
                    newRow.Cells("QUANTITY").Value = dr.Item("QUANTITY").ToString
                    newRow.Cells("TOTAL_ORG").Value = PRICE
                    newRow.Cells("VAT_PER").Value = VAT_PER
                    newRow.Cells("VAT_AMT").Value = PRICE * VAT_PER / 100
                    newRow.Cells("PRICE").Value = PRICE
                    newRow.EndEdit()
                Next
                UpdateTotal()
                Exit Sub
            End If
        End If

        Dim Col0 As GridEXColumn = Me.GridDetail.RootTable.Columns("ITEM_CODE")
        If GridDetail.FindAll(Col0, ConditionOperator.Equal, ITEM_CODE) > 0 _
                And ITEM_TYPE = 0 Then
            'Khong tang so luong doi voi mon Open Item va Set Item
            GridDetail.SetValue("QUANTITY", GridDetail.GetValue("QUANTITY") + 1)
            GridDetail.SetValue("TOTAL_ORG", GridDetail.GetValue("QUANTITY") * PRICE)
            GridDetail.SetValue("VAT_AMT", GridDetail.GetValue("QUANTITY") * PRICE * VAT_PER / 100)
            GridDetail.UpdateData()
        Else
            If ITEM_TYPE = 1 Then
                Dim frm1 As New frmOpenItem
                frm1.txtItemName.Text = ITEM_NAME
                If frm1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    ITEM_NAME = frm1.txtItemName.Text
                    PRICE = frm1.txtPrice.Value
                End If
            ElseIf ITEM_TYPE = 2 Then
                arName.Clear()
                arName.Add("@ITEM_CODE")
                arVal.Clear
                arVal.Add(ITEM_CODE)
                Dim dt1 As DataTable = BLL.ObjectGetBy("SUBITEM_LOADBYITEM", arName, arVal)
                If dt1.Rows.Count > 0 Then
                    Dim frm2 As New frmSUBITEM
                    frm2.GridSubItem.DataSource = dt1.DefaultView
                    If frm2.ShowDialog = Windows.Forms.DialogResult.OK Then
                        If GridDetail.RowCount > 0 Then
                            GridDetail.Row = GridDetail.RowCount - 1
                        End If

                        Dim LastBD_SEQ As Integer = GridDetail.GetValue("BD_SEQ")
                        LastBD_SEQ = LastBD_SEQ + 1
                        Dim PARENT_CODE As String = ITEM_CODE + "-" + CStr(LastBD_SEQ)

                        Dim newRow2 As GridEXRow = GridDetail.AddItem()
                        newRow2.BeginEdit()
                        newRow2.Cells("ITEM_CODE").Value = ITEM_CODE
                        newRow2.Cells("ITEM_NAME").Value = ITEM_NAME
                        newRow2.Cells("QUANTITY").Value = 1
                        newRow2.Cells("TOTAL_ORG").Value = PRICE
                        newRow2.Cells("VAT_PER").Value = VAT_PER
                        newRow2.Cells("VAT_AMT").Value = PRICE * VAT_PER / 100
                        newRow2.Cells("PRICE").Value = PRICE
                        newRow2.Cells("PARENT_CODE").Value = PARENT_CODE
                        newRow2.EndEdit()
                        newRow2.Expanded = True

                        For i As Integer = 0 To frm2.GridSelect.RowCount - 1
                            frm2.GridSelect.Row = i
                            Dim newRow1 As GridEXRow = GridDetail.AddItem(newRow2, newRow2.Table)
                            newRow1.BeginEdit()
                            newRow1.Cells("SUBITEM_CODE").Value = frm2.GridSelect.GetValue("SUBITEM_CODE").ToString
                            newRow1.Cells("ITEM_CODE").Value = ITEM_CODE
                            newRow1.Cells("ITEM_NAME").Value = frm2.GridSelect.GetValue("SUBITEM_NAME").ToString
                            newRow1.Cells("QUANTITY").Value = 1
                            newRow1.Cells("TOTAL_ORG").Value = 0
                            newRow1.Cells("VAT_PER").Value = 0
                            newRow1.Cells("VAT_AMT").Value = 0
                            newRow1.Cells("PRICE").Value = 0
                            newRow1.Cells("PARENT_CODE").Value = PARENT_CODE
                            newRow1.EndEdit()
                        Next
                        UpdateTotal()
                        Exit Sub
                    End If
                End If
            End If

            Dim newRow As GridEXRow = GridDetail.AddItem()
            newRow.BeginEdit()
            newRow.Cells("ITEM_CODE").Value = ITEM_CODE
            newRow.Cells("ITEM_NAME").Value = ITEM_NAME
            newRow.Cells("QUANTITY").Value = 1
            newRow.Cells("TOTAL_ORG").Value = PRICE
            newRow.Cells("VAT_PER").Value = VAT_PER
            newRow.Cells("VAT_AMT").Value = PRICE * VAT_PER / 100
            newRow.Cells("PRICE").Value = PRICE
            newRow.EndEdit()
        End If
        UpdateTotal()
    End Sub
    Private Sub UpdateTotal()
        Dim subtotal As Double = 0
        Dim DiscountAmt As Double = 0
        Dim total As Double = 0
        Dim TaxAmt As Double = 0
        GridDetail.UpdateData()
        subtotal = GridDetail.GetTotal(GridDetail.RootTable.Columns("TOTAL_ORG"), AggregateFunction.Sum)
        DiscountAmt = GridDetail.GetTotal(GridDetail.RootTable.Columns("DSC_AMT"), AggregateFunction.Sum)
        TaxAmt = GridDetail.GetTotal(GridDetail.RootTable.Columns("VAT_AMT"), AggregateFunction.Sum)
        total = subtotal + TaxAmt - DiscountAmt
        GridDetail.ExpandRecords()
        txtSubTotal.Text = String.Format(PublicVariable.NumberFormatString, subtotal)
        txtDsc_Amt.Text = String.Format(PublicVariable.NumberFormatString, DiscountAmt)
        txtTax.Text = String.Format(PublicVariable.NumberFormatString, TaxAmt)
        txtTotal.Text = String.Format(PublicVariable.NumberFormatString, total)
    End Sub
    Private Sub NewOrder()
        BILL_CODE = 0
        CustomerCode = ""
        EmployeeCode = ""
        GridDetail.ClearItems()
        txtCustomerName.Text = ""
        txtEmployee.Text = ""
        txtSubTotal.Text = "0"
        txtTotal.Text = "0"
        txtTax.Text = "0"
        txtDsc_Amt.Text = "0"
    End Sub
#End Region
   


    Private Sub GridDetail_CellUpdated(ByVal sender As System.Object, ByVal e As Janus.Windows.GridEX.ColumnActionEventArgs) Handles GridDetail.CellUpdated
        Select Case e.Column.Key
            Case "DSC_PER"
                If IsNumeric(GridDetail.GetValue("DSC_PER")) Then
                    Dim DSC_AMT As Double
                    Dim TOTAL_ORG As Double = GridDetail.GetValue("TOTAL_ORG")
                    Dim DSC_PER As Double = GridDetail.GetValue("DSC_PER")                   
                    DSC_AMT = TOTAL_ORG * DSC_PER / 100
                    GridDetail.SetValue("DSC_AMT", DSC_AMT)

                    Dim VAT_PER As Double = GridDetail.GetValue("VAT_PER")
                    Dim VAT_AMT As Double = (TOTAL_ORG - DSC_AMT) * VAT_PER / 100
                    GridDetail.SetValue("VAT_AMT", VAT_AMT)
                End If

            Case "DSC_AMT"
                If IsNumeric(GridDetail.GetValue("DSC_AMT")) Then
                    Dim DSC_PER As Double
                    Dim TOTAL_ORG As Double = GridDetail.GetValue("TOTAL_ORG")
                    Dim DSC_AMT As Double = GridDetail.GetValue("DSC_AMT")
                    DSC_PER = DSC_AMT * 100 / TOTAL_ORG
                    GridDetail.SetValue("DSC_PER", DSC_PER)

                    Dim VAT_PER As Double = GridDetail.GetValue("VAT_PER")
                    Dim VAT_AMT As Double = (TOTAL_ORG - DSC_AMT) * VAT_PER / 100
                    GridDetail.SetValue("VAT_AMT", VAT_AMT)
                End If
            Case "QUANTITY"
                If IsNumeric(GridDetail.GetValue("QUANTITY")) Then
                    Dim PRICE As Double = GridDetail.GetValue("PRICE")
                    Dim QTY As Double = GridDetail.GetValue("QUANTITY")
                    GridDetail.SetValue("TOTAL_ORG", PRICE * QTY)

                    Dim DSC_AMT As Double
                    Dim TOTAL_ORG As Double = PRICE * QTY
                    Dim DSC_PER As Double = GridDetail.GetValue("DSC_PER")
                    DSC_AMT = TOTAL_ORG * DSC_PER / 100
                    GridDetail.SetValue("DSC_AMT", DSC_AMT)

                    Dim VAT_PER As Double = GridDetail.GetValue("VAT_PER")
                    Dim VAT_AMT As Double = (TOTAL_ORG - DSC_AMT) * VAT_PER / 100
                    GridDetail.SetValue("VAT_AMT", VAT_AMT)
                End If
        End Select
        UpdateTotal()
    End Sub
End Class